<?php
	
	function isValidEmail($email){
		return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
	}
	function GetGps($loc){
		$loc=str_replace(' ','%20',$loc);
		$xml = file_get_contents('http://maps.googleapis.com/maps/api/geocode/xml?address='.$loc.'&sensor=false');
		preg_match('/<lat>(.*?)<\/lat>/', $xml, $lats);
		preg_match('/<lng>(.*?)<\/lng>/', $xml, $lons);
		return $lats[1].','.$lons[1];
	}
	
	// Make a MySQL Connection
	mysql_connect("localhost", "***", "***") or die(mysql_error());
	mysql_select_db("***") or die(mysql_error());

	
	if (isset($_POST['login'])) {
		
		$username = $_POST["username"];
		$password = $_POST["password"];
		$password = md5($password);
		if ($username != NULL && $password != NULL){
			$result = mysql_query("SELECT * FROM Users_table where username='$username'") or die(mysql_error());  
			if(mysql_num_rows($result)==0){
				echo "No such user";
			}
			else{
				$row = mysql_fetch_array( $result );
				if ($row['password'] == $password){
					setcookie("logged", 1, time()+60*60*24*100, "/");
					setcookie("username", $username, time()+60*60*24*100, "/");
					echo "<meta http-equiv='refresh' content='0'>";
				}
				else{
					echo "Wrong password";
				}
			}

		}
	}
	
	if (isset($_POST['register'])) { 
		
		$username = $_POST["username"];
		$email = $_POST["email"];
		$password = $_POST["password"];
		$password2 = $_POST["password2"];
		$password = md5($password);
		$password2 = md5($password2);
		if ($username != NULL && $email!= NULL && $password != NULL){
			if ($password== $password2){
				if (isValidEmail($email)){				
					mysql_query("INSERT INTO Users_table (username, email, password)
					VALUES ('$username', '$email','$password')");
					if (mysql_insert_id() == 0){
						echo ("Username already exists");
					}
					else{
						setcookie("logged", 1, time()+60*60*24*100, "/");
						setcookie("username", $username, time()+60*60*24*100, "/");
						echo "<meta http-equiv='refresh' content='0'>";
					}
					
				}
				else{
					echo "<b>Invalid email</b><br><br>";
				}
			}
			else{
				echo "<b>Passwords do not match</b><br><br>";
			}
		}
	}
	

   if($_COOKIE['logged'])
   {
		$name = $_COOKIE['username'];
		echo "Welcome $name";
		$result = mysql_query("SELECT * FROM Users_table where username='$name'") or die(mysql_error());  
		$row = mysql_fetch_array( $result );
		$uid = $row['user_id'];
		
		if (isset($_POST['delete'])) { 
			$reminderid = $_POST["reminderid"];
			if ($reminderid != NULL){		
				$result = mysql_query("SELECT * FROM Users_table where username='$name'") or die(mysql_error());  
				$row = mysql_fetch_array( $result );
				$uid = $row['user_id'];
				mysql_query("DELETE FROM Reminders_table WHERE reminder_id='$reminderid'");
			}
		}
		
		if (isset($_POST['add'])) { 
			$reminder_title = $_POST["reminder_title"];
			$reminder_text = $_POST["reminder_text"];
			$reminder_date = $_POST["reminder_date"];
			$general_loc = $_POST["general_loc"];
			
			$specific_loc = $_POST["specific_loc"];
			if ($specific_loc != NULL)
			{
				$gps=GetGps($specific_loc);
			}
			$time = strtotime( $reminder_date);
			if ($reminder_date == NULL){
				$date = 0;
			}
			else{
				$date = 1;
			}
			if ($reminder_title != NULL && $reminder_text != NULL){		
				mysql_query("INSERT INTO Reminders_table (reminder_title, reminder_text, user_id, time_boolean, reminder_time, general_location,coordinates,address)
				VALUES ('$reminder_title', '$reminder_text','$uid','$date','$time','$general_loc','$gps','$specific_loc')");
			}
		}

		//add reminder
		echo "<h1>Add reminder</h1>";
		echo "<form action='app.php' method='post'>";
		echo "Date: <input type='text' id='datetime' name='reminder_date' readonly/><br />";
		echo "Reminder title: <input type='text' name='reminder_title'><br />";
		echo "Reminder text: <textarea cols='20' rows='5' name='reminder_text'></textarea><br />";
		echo "Nearby reminder: <input type='text' name='general_loc'><br />";
		echo "Specific reminder: <input type='text' name='specific_loc'><br />";
		echo "<input type='submit' name='add' value='Add'/>";
		echo "</form>";
		echo "<br><br>";
		
		
		//display reminders
		
		echo "<h1>Delete</h1>";
		$result = mysql_query("SELECT * FROM Reminders_table where user_id='$uid'")
		or die(mysql_error());  
		
		echo "<form action='app.php' method='post'>";
		echo "<table border='1' cellpadding='10'>";
		echo "<tr><td>Select</td><td>Title</td><td>Reminder</td><td>Time</td><td>Nearby</td><td>Coords</td><td>Address</td></tr>";
		while ($row = mysql_fetch_array( $result )){
			echo "<tr>";
			$temp=$row['reminder_id'];
			echo"<td><input type='radio' name='reminderid' value='$temp' /></td>";
			echo "<td>".$row['reminder_title']."</td><td>".$row['reminder_text']."</td>";

			if ($row['time_boolean']){
				echo "<td>".date( 'y/m/d h:i a', $row['reminder_time'] )."</td>";
			}
			else{
				echo "<td></td>";
			}
			
			echo "<td>".$row['general_location']."</td>";
			echo "<td>".$row['coordinates']."</td>";
			echo "<td>".$row['address']."</td>";
			
			echo "</tr>";
		}
		echo "</table>";
		
		
		//delete reminder form
		echo "<input type='submit' name='delete' value='Delete'/>";
		echo "</form><br>";
		if ($name=='isaiah'){
			//refresh
			echo "<form action='app.php' method='post'>";		
			echo "<input type='submit' value='Refresh'/>";
			echo "</form>";
		}
		//logout
		echo "<form action='login.php' method='post'>";		
		echo "<input type='submit' name='logout' value='Log out'/>";
		echo "</form>";
		
		echo"<title>Taking Note</title>";
		
		
		
	}

	else{
		echo "<META HTTP-EQUIV='Refresh' CONTENT='2; URL=login.php'>";
	}
	
?>

	<link type="text/css" href="css/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet" />	
	<script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
	<script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>
	<script type="text/javascript" src="js/jquery-ui-timepicker-addon.js"></script>
		
	<script type="text/javascript">
		$(document).ready(function(){
			$( "#datetime" ).datetimepicker({ampm: true});
			
		});
	</script>
	